package com.example.vieweventdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

/**
 * Created by tangjiangxu on 9/26/17.
 */

public class HandlerPostDelayedActivity extends Activity{
    private static final int MESSAGE_SCROLL_TO = 1;
    private static final int FRAME_COUNT = 100;
    private static final int DELAYED_TIME = 16;

    private int mCount = 0;
    private TextView tv;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MESSAGE_SCROLL_TO:
                    mCount ++;
                    if (mCount < FRAME_COUNT) {
                        float fraction = mCount / (float)FRAME_COUNT;
                        int scrollX = (int) (fraction * 300);
                        int scrollY = (int) (fraction * 300);
                        tv.scrollTo(scrollX, scrollY);
                        handler.sendEmptyMessageDelayed(MESSAGE_SCROLL_TO, DELAYED_TIME);
                    }
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_post);
        tv = (TextView) findViewById(R.id.tv);
        handler.sendEmptyMessage(MESSAGE_SCROLL_TO);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(100);
                //finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setResult(1024);
    }
}
